using System.Collections;
using System.Collections.Generic;
using GameBaseFramework.Pattern;
using GameBaseFramework.Timer;
using Newtonsoft.Json;
using streaming;
using UnityEngine;

namespace GameLogics
{
    public class MainLogic
    {
        /// <summary>
        /// 表格数据
        /// </summary>
        public static cfg.Tables Tables { get; set; }
        /// <summary>
        /// 全局数据模块
        /// </summary>
        public static DataManager DataModuleManager { get; private set; }

        /// <summary>
        /// 配置：功能开关
        /// </summary>
        public static Dictionary<string, bool> Config = new();

        /// <summary>
        /// 本地保存的设置，音量等
        /// </summary>
        private static LocalSetting LocalSetting = new();

        /// <summary>
        /// 初始化
        /// </summary>
        public static void Init()
        {
            StreamingEventBus.Init();
            DataModuleManager = new DataManager();
        }

        /// <summary>
        /// 逻辑驱动
        /// </summary>
        public static void Update(float deltaTime)
        {
            TimerManager.Update(deltaTime);
            CommandManager.Update();
            DataModuleManager?.Update(deltaTime);
        }

        /// <summary>
        /// 获取本地设置
        /// </summary>
        public static LocalSetting GetLocalSetting()
        {
            if (LocalSetting != null) return LocalSetting;
            var localSettingstr = PlayerPrefs.GetString("LocalSetting", "");
            if (!string.IsNullOrEmpty(localSettingstr))
            {
                LocalSetting = JsonConvert.DeserializeObject<LocalSetting>(localSettingstr) ?? new LocalSetting();
            }
            else
            {
                LocalSetting = new LocalSetting();
            }
            return LocalSetting;
        }

        /// <summary>
        /// 保存本地设置
        /// </summary>
        /// <param name="localSetting"></param>
        public static void SaveLocalSetting(LocalSetting localSetting = null)
        {
            localSetting ??= LocalSetting;
            var localSettingStr = JsonConvert.SerializeObject(localSetting);
            PlayerPrefs.SetString("LocalSetting", localSettingStr);
        }
    }

}

